#include "lvgl/lvgl.h"

// 定义菜单项
typedef struct {
    const char* title;
    lv_obj_t* page;
} menu_item_t;

// 全局变量
static lv_obj_t* main_screen;
static lv_obj_t* settings_page;
static lv_obj_t* resolution_page;
static lv_obj_t* record_time_page;
static lv_obj_t* storage_page;

static lv_obj_t* main_menu;
static menu_item_t menu_items[] = {
    {"录像设置", NULL},
    {"存储管理", NULL},
    {"系统设置", NULL},
    {"关于设备", NULL}
};

// 分辨率选项
static const char* resolution_opts[] = {
    "2560x1440P30",
    "1920x1080P60",
    "1920x1080P30",
    "1280x720P60",
    NULL
};

// 录像时间选项
static const char* record_time_opts[] = {
    "1分钟",
    "3分钟",
    "5分钟",
    "10分钟",
    NULL
};

// 回调函数声明
static void main_menu_cb(lv_event_t* e);
static void resolution_select_cb(lv_event_t* e);
static void record_time_select_cb(lv_event_t* e);
static void format_confirm_cb(lv_event_t* e);
static void back_btn_cb(lv_event_t* e);

// 创建主界面
static void create_main_screen(void) 
{
    main_screen = lv_obj_create(NULL);
    
    // 创建状态栏
    lv_obj_t* status_bar = lv_obj_create(main_screen);
    lv_obj_set_size(status_bar, LV_HOR_RES, 30);
    lv_obj_align(status_bar, LV_ALIGN_TOP_MID, 0, 0);
    
    // 添加时间标签
    lv_obj_t* time_label = lv_label_create(status_bar);
    lv_label_set_text(time_label, "12:00:00");
    lv_obj_align(time_label, LV_ALIGN_LEFT_MID, 10, 0);
    
    // 添加录像状态图标
    lv_obj_t* rec_icon = lv_label_create(status_bar);
    lv_label_set_text(rec_icon, LV_SYMBOL_RECORD);
    lv_obj_align(rec_icon, LV_ALIGN_RIGHT_MID, -10, 0);
    
    // 创建预览窗口
    lv_obj_t* preview = lv_obj_create(main_screen);
    lv_obj_set_size(preview, LV_HOR_RES, LV_VER_RES - 80);
    lv_obj_align(preview, LV_ALIGN_TOP_MID, 0, 30);
    
    // 创建底部工具栏
    lv_obj_t* toolbar = lv_obj_create(main_screen);
    lv_obj_set_size(toolbar, LV_HOR_RES, 50);
    lv_obj_align(toolbar, LV_ALIGN_BOTTOM_MID, 0, 0);
    
    // 添加菜单按钮
    lv_obj_t* menu_btn = lv_btn_create(toolbar);
    lv_obj_set_size(menu_btn, 80, 40);
    lv_obj_align(menu_btn, LV_ALIGN_LEFT_MID, 10, 0);
    lv_obj_add_event_cb(menu_btn, main_menu_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t* menu_label = lv_label_create(menu_btn);
    lv_label_set_text(menu_label, "菜单");
    lv_obj_center(menu_label);
}

// 创建设置菜单页面
static void create_settings_page(void)
{
    settings_page = lv_obj_create(NULL);
    
    // 创建标题栏
    lv_obj_t* title = lv_label_create(settings_page);
    lv_label_set_text(title, "设置");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
    
    // 创建列表
    lv_obj_t* list = lv_list_create(settings_page);
    lv_obj_set_size(list, LV_HOR_RES - 40, LV_VER_RES - 80);
    lv_obj_align(list, LV_ALIGN_TOP_MID, 0, 40);
    
    // 添加列表项
    for(int i = 0; i < sizeof(menu_items)/sizeof(menu_item_t); i++) {
        lv_obj_t* btn = lv_list_add_btn(list, NULL, menu_items[i].title);
        lv_obj_add_event_cb(btn, main_menu_cb, LV_EVENT_CLICKED, (void*)i);
    }
    
    // 添加返回按钮
    lv_obj_t* back_btn = lv_btn_create(settings_page);
    lv_obj_set_size(back_btn, 80, 40);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_LEFT, 10, -10);
    lv_obj_add_event_cb(back_btn, back_btn_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t* back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
}

// 创建分辨率设置页面
static void create_resolution_page(void)
{
    resolution_page = lv_obj_create(NULL);
    
    // 创建标题
    lv_obj_t* title = lv_label_create(resolution_page);
    lv_label_set_text(title, "分辨率设置");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
    
    // 创建分辨率选择器
    lv_obj_t* dropdown = lv_dropdown_create(resolution_page);
    lv_dropdown_set_options(dropdown, "2560x1440P30\n"
                                    "1920x1080P60\n"
                                    "1920x1080P30\n"
                                    "1280x720P60");
    lv_obj_set_size(dropdown, 200, 40);
    lv_obj_align(dropdown, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_add_event_cb(dropdown, resolution_select_cb, LV_EVENT_VALUE_CHANGED, NULL);
    
    // 添加返回按钮
    lv_obj_t* back_btn = lv_btn_create(resolution_page);
    lv_obj_set_size(back_btn, 80, 40);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_LEFT, 10, -10);
    lv_obj_add_event_cb(back_btn, back_btn_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t* back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
}

// 创建录像时间设置页面
static void create_record_time_page(void)
{
    record_time_page = lv_obj_create(NULL);
    
    // 创建标题
    lv_obj_t* title = lv_label_create(record_time_page);
    lv_label_set_text(title, "录像时长设置");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
    
    // 创建时间选择器
    lv_obj_t* dropdown = lv_dropdown_create(record_time_page);
    lv_dropdown_set_options(dropdown, "1分钟\n"
                                    "3分钟\n"
                                    "5分钟\n"
                                    "10分钟");
    lv_obj_set_size(dropdown, 200, 40);
    lv_obj_align(dropdown, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_add_event_cb(dropdown, record_time_select_cb, LV_EVENT_VALUE_CHANGED, NULL);
    
    // 添加返回按钮
    lv_obj_t* back_btn = lv_btn_create(record_time_page);
    lv_obj_set_size(back_btn, 80, 40);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_LEFT, 10, -10);
    lv_obj_add_event_cb(back_btn, back_btn_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t* back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
}

// 创建存储管理页面
static void create_storage_page(void)
{
    storage_page = lv_obj_create(NULL);
    
    // 创建标题
    lv_obj_t* title = lv_label_create(storage_page);
    lv_label_set_text(title, "存储管理");
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
    
    // 创建存储信息
    lv_obj_t* storage_info = lv_label_create(storage_page);
    lv_label_set_text(storage_info, "存储卡容量: 32GB\n已用空间: 16GB\n可用空间: 16GB");
    lv_obj_align(storage_info, LV_ALIGN_TOP_MID, 0, 50);
    
    // 创建格式化按钮
    lv_obj_t* format_btn = lv_btn_create(storage_page);
    lv_obj_set_size(format_btn, 120, 40);
    lv_obj_align(format_btn, LV_ALIGN_CENTER, 0, 50);
    lv_obj_add_event_cb(format_btn, format_confirm_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t* format_label = lv_label_create(format_btn);
    lv_label_set_text(format_label, "格式化");
    lv_obj_center(format_label);
    
    // 添加返回按钮
    lv_obj_t* back_btn = lv_btn_create(storage_page);
    lv_obj_set_size(back_btn, 80, 40);
    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_LEFT, 10, -10);
    lv_obj_add_event_cb(back_btn, back_btn_cb, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t* back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回");
    lv_obj_center(back_label);
}

// 回调函数实现
static void main_menu_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        uint32_t idx = (uint32_t)lv_event_get_user_data(e);
        switch(idx) {
            case 0: // 录像设置
                lv_scr_load(resolution_page);
                break;
            case 1: // 存储管理
                lv_scr_load(storage_page);
                break;
            // ... 其他菜单项处理
        }
    }
}

static void resolution_select_cb(lv_event_t* e)
{
    lv_obj_t* dropdown = lv_event_get_target(e);
    char buf[32];
    lv_dropdown_get_selected_str(dropdown, buf, sizeof(buf));
    // 处理分辨率选择
}

static void record_time_select_cb(lv_event_t* e)
{
    lv_obj_t* dropdown = lv_event_get_target(e);
    char buf[32];
    lv_dropdown_get_selected_str(dropdown, buf, sizeof(buf));
    // 处理录像时间选择
}

static void format_confirm_cb(lv_event_t* e)
{
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        // 创建确认对话框
        static const char* btns[] = {"确认", "取消", ""};
        lv_obj_t* mbox = lv_msgbox_create(NULL, "警告", 
            "确定要格式化存储卡吗？\n此操作将删除所有数据！", btns, true);
        lv_obj_center(mbox);
    }
}

static void back_btn_cb(lv_event_t* e)
{
    if(lv_event_get_code(e) == LV_EVENT_CLICKED) {
        lv_scr_load(main_screen);
    }
}

// 初始化UI
void init_car_dvr_ui(void)
{
    // 创建所有页面
    create_main_screen();
    create_settings_page();
    create_resolution_page();
    create_record_time_page();
    create_storage_page();
    
    // 加载主屏幕
    lv_scr_load(main_screen);
} 